import api from "../request/apis.js";
import moment from "moment";
import {info} from "./logview/index.js";

export const appointmentInject = async (resultMap, startDate) => {
    const deptIds = [];
    for (const result of resultMap) {
        const deptId = result[0];
        deptIds.push(deptId);
    }
    //先查询地址
    const locationList = await api.listLocationByType(deptIds);

    //查询挂号类别
    const registerCategoryList = await api.queryRegisterCategoryList();
    const ms = [];
    for (const registerCategory of registerCategoryList) {
        ms.push(registerCategory.code);
    }
    //查询挂号级别
    const registerLevelList = await api.queryRegisterLevelList(ms);
    //查询班次
    const shiftList = await api.getShiftList();


    const deptIdList = [];
    for (const result of resultMap) {
        const deptId = result[0];
        deptIdList.push(deptId);
    }

    //再查一下
    const databaseDoctorInfo = await api.listDoctorByDeptIds(deptIdList);

    const deptMap = new Map();
    for (const result of resultMap) {
        const deptId = result[0];
        const doctorList = result[1];
        //设置班次
        beforeProcessor(doctorList, databaseDoctorInfo);

        setShiftId(doctorList, shiftList);
        //设置一把诊区
        setLocationId(doctorList, locationList);

        //设置一把挂号类别
        setRegisterCategoryId(doctorList, registerCategoryList);

        //设置挂号级别
        setRegisterLevelId(doctorList, registerLevelList);

        //设置班次
        setShiftId(doctorList, shiftList);

        //设置班次
        setWeek(doctorList, startDate);

        const doctors = getDoctors(doctorList, startDate);
        deptMap.set(deptId, doctors);
    }

    return deptMap;


}

const beforeProcessor = (doctorList, databaseDoctorInfo) => {
    for (const doctor of doctorList) {

        let doctorId = doctor.doctorId;
        if (doctorId && doctorId.indexOf("（") != -1) {
            doctorId = doctorId.substring(0, doctorId.indexOf("（"));
            doctor.doctorId = doctorId;
        }
        //重新设置一把员工ID
        for (const dc of databaseDoctorInfo) {
            if (dc.accountNo == doctor.doctorId) {
                doctor.doctorId = dc.id;
            }
        }


        let registerCategoryName = doctor.registerCategoryName;
        if (registerCategoryName && registerCategoryName.indexOf("（") != -1) {
            registerCategoryName = registerCategoryName.substring(0, registerCategoryName.indexOf("（"));
            doctor.registerCategoryName = registerCategoryName;
        }
        if (!registerCategoryName) {
            info(`科室【${doctor.deptName},${doctor.deptId}】医生【${doctor.doctorName},${doctor.doctorId}】没有设置挂号类别`);
        }

        let registerLevelName = doctor.registerLevelName;

        if (!registerLevelName) {
            info(`科室【${doctor.deptName},${doctor.deptId}】医生【${doctor.doctorName},${doctor.doctorId}】没有设置挂号级别`);
        }


        const c = localStorage.getItem("registerLevelMapping");
        if (c) {
            const mappings = JSON.parse(c);
            for (const mapping of mappings) {
                const sourceName = mapping.sourceName;
                const targetName = mapping.targetName;
                let findFlag = false;
                for (const s of sourceName) {
                    if (registerLevelName == s) {
                        findFlag = true;
                        break;
                    }
                }
                if (findFlag) {
                    doctor.registerLevelName = targetName;
                    break
                }

            }
        }


        // if (registerLevelName && registerLevelName == '主治医师') {
        //     registerLevelName = '普通门诊诊查费';
        //     doctor.registerLevelName = registerLevelName;
        // }
        // if (registerLevelName && registerLevelName == '住院医师') {
        //     registerLevelName = '普通门诊诊查费';
        //     doctor.registerLevelName = registerLevelName;
        // }
        // if (registerLevelName && registerLevelName == '普通') {
        //     registerLevelName = '普通门诊诊查费';
        //     doctor.registerLevelName = registerLevelName;
        // }
        // if (registerLevelName && registerLevelName == '副主任医师') {
        //     registerLevelName = '专家门诊诊查费（副主任医师）';
        //     doctor.registerLevelName = registerLevelName;
        // }
        // if (registerLevelName && registerLevelName == '副主任医师（10.5）') {
        //     registerLevelName = '专家门诊诊查费（副主任医师）';
        //     doctor.registerLevelName = registerLevelName;
        // }
        // if (registerLevelName && registerLevelName == '知名专家') {
        //     registerLevelName = '专家门诊诊查费(知名专家)';
        //     doctor.registerLevelName = registerLevelName;
        // }
        // if (registerLevelName && registerLevelName == '知名专家（20.5）') {
        //     registerLevelName = '专家门诊诊查费(知名专家)';
        //     doctor.registerLevelName = registerLevelName;
        // }
        // if (registerLevelName && registerLevelName == '普通门诊') {
        //     registerLevelName = '普通门诊诊查费';
        //     doctor.registerLevelName = registerLevelName;
        // }
        // if (registerLevelName && registerLevelName == '普通门诊（5.5）') {
        //     registerLevelName = '普通门诊诊查费';
        //     doctor.registerLevelName = registerLevelName;
        // }
        // if (registerLevelName && registerLevelName == '主任医师') {
        //     registerLevelName = '专家门诊诊查费_主任医师';
        //     doctor.registerLevelName = registerLevelName;
        // }
        // if (registerLevelName && registerLevelName == '主任医师（12.5）') {
        //     registerLevelName = '专家门诊诊查费_主任医师';
        //     doctor.registerLevelName = registerLevelName;
        // }
        //
        // if (registerLevelName && registerLevelName == '专家门诊诊查费(知名专家)') {
        //     registerLevelName = '专家门诊诊查费(知名专家)（20.5）';
        //     doctor.registerLevelName = registerLevelName;
        //     continue;
        // }
        //
        // if (registerLevelName && registerLevelName == '专家门诊诊查费（副主任医师）') {
        //     registerLevelName = '专家门诊诊查费（副主任医师）（10.5）';
        //     doctor.registerLevelName = registerLevelName;
        //     continue;
        // }
        //
        // if (registerLevelName && registerLevelName == '普通门诊诊查费') {
        //     registerLevelName = '普通门诊诊查费（5.5）';
        //     doctor.registerLevelName = registerLevelName;
        //     continue;
        // }
        // if (registerLevelName && registerLevelName == '普通门诊诊查费') {
        //     registerLevelName = '普通门诊诊查费（5.5）';
        //     doctor.registerLevelName = registerLevelName;
        //     continue;
        // }
        //
        // if (registerLevelName && registerLevelName == '特需门诊诊查费（西医）') {
        //     registerLevelName = '特需门诊诊查费（西医）（100）';
        //     doctor.registerLevelName = registerLevelName;
        //     continue;
        // }
        //
        //
        // if (registerLevelName && registerLevelName.indexOf("（") != -1) {
        //     let c = registerLevelName.substring(registerLevelName.lastIndexOf("（"));
        //     if (c.indexOf("西医") != -1 || c.indexOf("中医") != -1 || c.indexOf("产科") != -1 || c.indexOf("妇科") != -1
        //         || c.indexOf("心理咨询") != -1 || c.indexOf("副主任医师") != -1
        //         || c.indexOf("产科") != -1) {
        //         continue;
        //     }
        //     registerLevelName = registerLevelName.substring(0, registerLevelName.lastIndexOf("（"));
        //     doctor.registerLevelName = registerLevelName;
        // }
    }
}

const getDoctors = (doctorList, startDate) => {
    const currentDay = new Date(startDate);

    const week = weekCheck(currentDay);


    const dc = [];
    for (const doctor of doctorList) {
        if (doctor.day1) {
            //设置服务日期
            const start = moment(currentDay).add(week - 1, 'days').format('YYYY-MM-DD');
            const s = JSON.parse(JSON.stringify(doctor));
            delete s.day1;
            delete s.day2;
            delete s.day3;
            delete s.day4;
            delete s.day5;
            delete s.day6;
            delete s.day7;
            s.week = 1;
            s.serviceDate = start;
            s.load = doctor.day1;
            dc.push(s);
        }
        if (doctor.day2) {
            const start = moment(currentDay).subtract(week - 2, 'days').format('YYYY-MM-DD');
            const s = JSON.parse(JSON.stringify(doctor));
            delete s.day1;
            delete s.day2;
            delete s.day3;
            delete s.day4;
            delete s.day5;
            delete s.day6;
            delete s.day7;
            s.serviceDate = start;
            s.week = 2;
            s.load = doctor.day2;
            dc.push(s);
        }
        if (doctor.day3) {
            const start = moment(currentDay).subtract(week - 3, 'days').format('YYYY-MM-DD');
            const s = JSON.parse(JSON.stringify(doctor));
            delete s.day1;
            delete s.day2;
            delete s.day3;
            delete s.day4;
            delete s.day5;
            delete s.day6;
            delete s.day7;
            s.week = 3;
            s.serviceDate = start;
            s.load = doctor.day3;
            dc.push(s);
        }
        if (doctor.day4) {
            const start = moment(currentDay).subtract(week - 4, 'days').format('YYYY-MM-DD');
            const s = JSON.parse(JSON.stringify(doctor));
            delete s.day1;
            delete s.day2;
            delete s.day3;
            delete s.day4;
            delete s.day5;
            delete s.day6;
            delete s.day7;
            s.week = 4;
            s.serviceDate = start;
            s.load = doctor.day4;
            dc.push(s);
        }

        if (doctor.day5) {
            const start = moment(currentDay).subtract(week - 5, 'days').format('YYYY-MM-DD');
            const s = JSON.parse(JSON.stringify(doctor));
            delete s.day1;
            delete s.day2;
            delete s.day3;
            delete s.day4;
            delete s.day5;
            delete s.day6;
            delete s.day7;
            s.week = 5;
            s.serviceDate = start;
            s.load = doctor.day5;
            dc.push(s);
        }

        if (doctor.day6) {
            const start = moment(currentDay).subtract(week - 6, 'days').format('YYYY-MM-DD');
            const s = JSON.parse(JSON.stringify(doctor));
            delete s.day1;
            delete s.day2;
            delete s.day3;
            delete s.day4;
            delete s.day5;
            delete s.day6;
            delete s.day7;
            s.week = 6;
            s.serviceDate = start;
            s.load = doctor.day6;
            dc.push(s);
        }

        if (doctor.day7) {
            const start = moment(currentDay).add(7 - week, 'days').format('YYYY-MM-DD');
            const s = JSON.parse(JSON.stringify(doctor));
            delete s.day1;
            delete s.day2;
            delete s.day3;
            delete s.day4;
            delete s.day5;
            delete s.day6;
            delete s.day7;
            s.week = 7;
            s.serviceDate = start;
            s.load = doctor.day7;
            dc.push(s);
        }
    }

    return dc;
}


const setWeek = (doctorList, startDate) => {

    const currentDay = new Date(startDate);

    const week = weekCheck(currentDay);

    //获取今天星期
    for (const doctor of doctorList) {
        //把之前的制空
        if (week == "1") {

        } else if (week == "2") {
            doctor.day1 = null;
        } else if (week == "3") {
            doctor.day1 = null;
            doctor.day2 = null;
        } else if (week == "4") {
            doctor.day1 = null;
            doctor.day2 = null;
            doctor.day3 = null;
        } else if (week == "5") {
            doctor.day1 = null;
            doctor.day2 = null;
            doctor.day3 = null;
            doctor.day4 = null;
        } else if (week == "6") {
            doctor.day1 = null;
            doctor.day2 = null;
            doctor.day3 = null;
            doctor.day4 = null;
            doctor.day5 = null;
        } else if (week == "7") {
            doctor.day1 = null;
            doctor.day2 = null;
            doctor.day3 = null;
            doctor.day4 = null;
            doctor.day5 = null;
            doctor.day6 = null;
        }
        // if (doctor.day1) {
        //     doctor.serviceDate = moment(currentDay).format("YYYY-MM-DD");
        // }
    }

}
const weekCheck = (date) => {
    let s = moment(date).format("E");
    return s;
}

const setShiftId = (doctorList, shiftList) => {
    for (const doctor of doctorList) {
        const shiftName = doctor.shiftName;
        for (const shift of shiftList) {
            if (shift.name == shiftName) {
                doctor.shiftId = shift.shiftId;
                const beginTime = shift.shiftSeasons[0]?.shiftTimeFrameList[0]?.beginTime;
                const endTime = shift.shiftSeasons[0]?.shiftTimeFrameList[0]?.endTime;
                doctor.beginTime = beginTime;
                doctor.endTime = endTime;
                break;
            }
        }
    }

}


const setRegisterLevelId = (doctorList, registerLevelList) => {
    for (const doctor of doctorList) {
        let registerLevelName = doctor.registerLevelName;
        for (const registerLevel of registerLevelList) {
            if (registerLevel.name == registerLevelName) {
                doctor.registerLevelId = registerLevel.id;
                break;
            }
        }
    }
}

const setRegisterCategoryId = (doctorList, registerCategoryList) => {
    for (const doctor of doctorList) {
        let registerCategoryName = doctor.registerCategoryName;
        for (const registerCategory of registerCategoryList) {
            if (registerCategory.name == registerCategoryName) {
                doctor.registerCategoryCode = registerCategory.code;
                break;
            }
        }
    }
}

const setLocationId = (doctorList, locationList) => {
    for (const doctor of doctorList) {
        const deptId = doctor.deptId;
        const locationName = doctor.locationName;
        const location = findLocationList(deptId, locationName, locationList);
        if (!location) {
            info(`科室【${doctor.deptName}】【${deptId}】【${locationName}】获取诊区失败`);
        }
        doctor.locationId = location.id;
    }
}


const findLocationList = (deptId, locationName, locationList) => {

    let location = null;
    for (const doctor of locationList) {

        if (!doctor.unitIds || doctor.unitIds.length <= 0) {
            continue;
        }
        if (doctor.name != locationName) {
            continue;
        }
        let i = 0;
        for (const staffUnit of doctor.unitIds) {
            if (staffUnit == deptId) {
                i++;
            }
        }
        if (i > 0) {
            location = doctor;
            break;
        }
    }
    return location;

}